package mms.tooleditor;

import com.mxgraph.analysis.mxAnalysisGraph;
import com.mxgraph.analysis.mxTraversal;
import com.mxgraph.model.mxCell;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
 *
 */
public class CodeGenerator extends AbstractAction {

    private mxAnalysisGraph aGraph;

    /**
     * Examples for calling analysis methods from mxGraphStructure
     */
    public CodeGenerator(mxAnalysisGraph aGraph) {
        this.aGraph = aGraph;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof mxGraphComponent) {
            mxGraphComponent graphComponent = (mxGraphComponent) e.getSource();
            mxGraph graph = graphComponent.getGraph();
            aGraph.setGraph(graph);
//                Object startVertex = mxGraphStructure.getVertexWithValue(aGraph, 0);
            System.out.println("Selection: " + graph.getSelectionCell());
            Object[] vertices = graph.getChildVertices(graph.getDefaultParent());
            mxTraversal.bfs(aGraph, vertices[0], new mxGraph.mxICellVisitor() {
                // simple visitor that prints current vertex
                public boolean visit(Object vertex, Object edge) {
                    mxCell v = (mxCell) vertex;
                    mxCell e = (mxCell) edge;

                    if (e != null) {
                        System.out.println("Vertex: " + v.getValue() + " edge: " + e.getValue());
                    } else {
                        System.out.println("Vertex: " + v.getValue() + " edge: N/A");
                    }

                    return false;
                }
            });
        }

    }

    ;
}
